package com.sysd.server.code;

import com.alibaba.fastjson2.JSONObject;
import com.sysd.bean.enums.MessageTypeEnum;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.List;
import java.util.Optional;

/**
 * @description:
 * @author: bsh
 * @date: 2025/6/12 15:30
 */
public class MessageDecode extends MessageToMessageDecoder<TextWebSocketFrame> {
    /**
     * @param channelHandlerContext 上下文
     * @param textWebSocketFrame    消息
     * @param list                  处理链表
     * @throws Exception 异常
     */
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame, List<Object> list) throws Exception {
        String text = textWebSocketFrame.text();
        JSONObject json = JSONObject.parseObject(text);
        Integer type = Optional.ofNullable(json.getInteger("type")).orElse(-1);
        if (type >= 0) {
            list.add(MessageTypeEnum.res(type).apply(json));
        }
    }
}
